package com.yf.login;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyOpenHelper extends SQLiteOpenHelper {

	public MyOpenHelper(Context context) {
		/**
		 * context:当前应用的上下文对象 name: 数据库的名字，null表示在内存中创建临时数据库，给定名称则创建永久数据库
		 * factory: 游标工厂，null表示使用系统默认游标工厂 version: 数据库版本号，从1开始，版本号用来控制数据库的升级和降级
		 */
		super(context, "test.db", null, 2);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 数据库文件创建的时候此方法被系统调用
		// sqlite中id之类的列名习惯使用下划线开头，如:_id
		String sql = "CREATE TABLE user(_id integer primary key autoincrement,"
				+ "name varchar(20),phone varchar(12),amount double)";
		db.execSQL(sql);
		Log.v("MyOpenHelper", "表user创建完成");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.v("MyOpenHelper", "onUpgrade：　" + String.valueOf(oldVersion));
		Log.v("MyOpenHelper", "onUpgrade：　" + String.valueOf(newVersion));
		// db.execSQL("alter table user add age integer;");
	}

	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		super.onDowngrade(db, oldVersion, newVersion);
		Log.v("MyOpenHelper", "onDowngrade: " + String.valueOf(oldVersion));
		Log.v("MyOpenHelper", "onDowngrade: " + String.valueOf(newVersion));
	}

}
